package com.yu.util;

import lombok.experimental.UtilityClass;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author : wenchao.long
 * @date : Created in 2021/01/05 10:56 周二
 * @description : webService 上下文工具
 */
@UtilityClass
public class WebServiceContextUtil {

    /**
     * 获取message
     *
     * @return Message
     */
    public static Message getMessage() {
        return PhaseInterceptorChain.getCurrentMessage();
    }

    /**
     * 获取Request
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) getMessage().get(AbstractHTTPDestination.HTTP_REQUEST);
    }

    /**
     * 获取Response
     *
     * @return HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        return (HttpServletResponse) getMessage().get(AbstractHTTPDestination.HTTP_RESPONSE);
    }

    /**
     * 获取session
     *
     * @return HttpSession
     */
    public static HttpSession getHttpSession() {
        return getRequest().getSession();
    }

    /**
     * 获取远程地址
     *
     * @return String
     */
    public static String getRemoteAddr() {
        return getRequest().getRemoteAddr();
    }

    /**
     * 获取 某个header值
     *
     * @param key header
     * @return String
     */
    public static String getHeader(String key) {
        return getRequest().getHeader(key);
    }

    /**
     * 获取某个属性
     *
     * @param key key
     * @return Object
     */
    public static Object getAttribute(String key) {
        return getRequest().getAttribute(key);
    }

}
